package ${cfg.packageController};


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${cfg.packageEntity}.${cfg.entity.entityName};
import ${cfg.packageEntity}.dto.${cfg.entity.entityName}DTO;
import ${cfg.packageEntity}.insert.${cfg.entity.entityName}InsertDTO;
import ${cfg.packageEntity}.update.${cfg.entity.entityName}UpdateDTO;
import com.ezbuild.generator.service.${cfg.entity.entityName}Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * ${cfg.entity.comment} 前端控制器
 * </p>
 *
 * @author wandoupeas
 * @version 0.0.1
 * @date ${.now?string["yyyy-MM-dd HH:mm:ss"]}
 * @since ${.now?string["yyyy-MM-dd HH:mm:ss"]}
 */
@RestController
@RequestMapping("/${cfg.mappingName}")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${cfg.entity.entityName}Controller {
    private final ${cfg.entity.entityName}Service ${cfg.entity.entityName?uncap_first}Service;

    @GetMapping
    public R<${cfg.entity.entityName}> get(${cfg.entity.entityName}DTO ${cfg.entity.entityName?uncap_first}DTO) {
        QueryWrapper<${cfg.entity.entityName}> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(${cfg.entity.entityName?uncap_first}DTO.getId() != null, ${cfg.entity.entityName}::getId, ${cfg.entity.entityName?uncap_first}DTO.getId());
        ${cfg.entity.entityName} ${cfg.entity.entityName?uncap_first} = ${cfg.entity.entityName?uncap_first}Service.getOne(wrapper);
        return R.ok(${cfg.entity.entityName?uncap_first});
    }

    @GetMapping("/list")
    public R<List<${cfg.entity.entityName}>> list(${cfg.entity.entityName}DTO ${cfg.entity.entityName?uncap_first}DTO) {
        QueryWrapper<${cfg.entity.entityName}> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(${cfg.entity.entityName?uncap_first}DTO.getId() != null, ${cfg.entity.entityName}::getId, ${cfg.entity.entityName?uncap_first}DTO.getId());
        List<${cfg.entity.entityName}> list = ${cfg.entity.entityName?uncap_first}Service.list(wrapper);
        return R.ok(list);
    }

    @GetMapping("/page")
    public R<IPage<${cfg.entity.entityName}>> page(${cfg.entity.entityName}DTO ${cfg.entity.entityName?uncap_first}DTO) {
        Page<${cfg.entity.entityName}> ${cfg.entity.entityName?uncap_first}Page = new Page<>(${cfg.entity.entityName?uncap_first}DTO.getPage(), ${cfg.entity.entityName?uncap_first}DTO.getLimit(), true);
        QueryWrapper<${cfg.entity.entityName}> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${cfg.entity.entityName?uncap_first}DTO.getId() != null, ${cfg.entity.entityName}::getId, ${cfg.entity.entityName?uncap_first}DTO.getId());
        if (StrUtil.isNotBlank(${cfg.entity.entityName?uncap_first}DTO.getSortBy())) {
            if (${cfg.entity.entityName?uncap_first}DTO.getDescending()) {
                wrapper.orderByDesc(${cfg.entity.entityName?uncap_first}DTO.getSortBy());
            } else if (!${cfg.entity.entityName?uncap_first}DTO.getDescending()) {
                wrapper.orderByAsc(${cfg.entity.entityName?uncap_first}DTO.getSortBy());
            }
        }
        return R.ok(${cfg.entity.entityName?uncap_first}Service.page(${cfg.entity.entityName?uncap_first}Page, wrapper));
    }

    @PostMapping
    public R<String> insert(@RequestBody ${cfg.entity.entityName}InsertDTO ${cfg.entity.entityName?uncap_first}InsertDTO) {
        return ${cfg.entity.entityName?uncap_first}Service.save(JSONUtil.toBean(JSONUtil.parseObj(${cfg.entity.entityName?uncap_first}InsertDTO), ${cfg.entity.entityName}.class)) ? R.ok("新增成功") : R.failed("新增失败");
    }

    @PutMapping
    public R<String> update(@RequestBody ${cfg.entity.entityName}UpdateDTO ${cfg.entity.entityName?uncap_first}UpdateDTO) {
        return ${cfg.entity.entityName?uncap_first}Service.updateById(JSONUtil.toBean(JSONUtil.parseObj(${cfg.entity.entityName?uncap_first}UpdateDTO), ${cfg.entity.entityName}.class)) ? R.ok("修改成功") : R.failed("修改失败");
    }

    @DeleteMapping
    public R<String> delete(Integer id) {
        return ${cfg.entity.entityName?uncap_first}Service.removeById(id) ? R.ok("删除成功") : R.failed("删除失败");
    }
}
